package com.kfm.spring.test;

import com.kfm.spring.dao.IAccountDao;
import com.kfm.spring.dao.impl.AccountDaoImpl;
import com.kfm.spring.service.IAccountService;
import com.kfm.spring.service.impl.AccountServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {


    @Test
    public void getBean(){
        // 1. 获取spring IOC容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // 2. 根据id获取bean对象
        Object accountDao = context.getBean("accountDao");
        IAccountDao bean = context.getBean(IAccountDao.class);
        System.out.println(bean);
        System.out.println(accountDao);
        System.out.println(bean == accountDao);

        System.out.println(bean.getInfo());
        context.close();
    }

    @Test
    public void getBoyBean(){
        // 1. 获取spring IOC容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // 2. 根据id获取bean对象
        Object bean = context.getBean("boy");
        Object bean1 = context.getBean("boy");

        System.out.println(bean);
        System.out.println(bean1);
        System.out.println(bean1 == bean);
    }

    @Test
    public void getServiceBean(){
        // 1. 获取spring IOC容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // 2. 根据id获取bean对象
        IAccountService accountService = context.getBean("accountService", IAccountService.class);

        accountService.saveAccount();
    }
}
